const express=require('express');
const router=express.Router();
const query = require('../mysql')
const jwt= require('../utils/jwt')
router.get('/getMeeting',jwt.auth,function(req,res){
        const page = (req.query.pageNum-1)*req.query.pageSize
        var  sql = `SELECT *,(SELECT COUNT(*) FROM my_meeting WHERE creatorId = '${req.user.id}') AS total FROM my_meeting WHERE creatorId = '${req.user.id}' limit ${page},${req.query.pageSize}`;
        query(sql,function (err,result,) {
            if(err){ //sql 语句报错
                res.send({code:202,data:err,msg:"错误"})
                return;
            }else{

                const data ={list:result,total:result[0]?.total || 0}
                res.send({code:200,data:data,msg:"ok"})
            }
        })
});
router.post('/addMeeting',jwt.auth,function(req,res){
  console.log(req.user);
  const data = req.body
  var  sql = `INSERT INTO my_meeting (meetingName, location, toastmaster, toastmasterId, participant, participantId,creator,creatorId,startTime,endTime)
      VALUES ('${data.meetingName}', '${data.location}', '${data.toastmaster}', '${data.toastmasterId}','${data.participant}', '${data.participantId}','${req.user.name}','${req.user.id}','${data.startTime}','${data.endTime}')`;
  query(sql,function (err,result,) {
      if(err){ //sql 语句报错
          res.send({code:202,data:err,msg:"错误"})
          return;
      }else{
          res.send({code:200,data:true,msg:"ok"})
      }
  })
});
router.post('/delMeeting',function(req,res){
  console.log(req.body);
  const data = req.body
  var  sql = `DELETE FROM my_meeting WHERE id = '${data.id}'`;
  query(sql,function (err,result,) {
      if(err){ //sql 语句报错
          res.send({code:202,data:err,msg:"错误"})
          return;
      }
      if(result.length==0){
          res.send({code:201,data:result,msg:"ok"})
      }else{
          res.send({code:200,data:result,msg:"ok"})
      }
  })
});
module.exports = router
